package com.test.webdemo.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class FullLifecycleTrackingBean implements
        BeanNameAware, BeanFactoryAware, ApplicationContextAware,
        InitializingBean, DisposableBean {

    private String name;

    // 1. 构造方法
    public FullLifecycleTrackingBean() {
        System.out.println("1. 【构造阶段】构造函数执行 - Bean实例化");
    }

    // 2. 属性注入
    public void setName(String name) {
        this.name = name;
        System.out.println("2. 【依赖注入】属性设置: name = " + name);
    }

    // 3. BeanNameAware
    @Override
    public void setBeanName(String name) {
        System.out.println("3. 【Aware接口】BeanNameAware.setBeanName: " + name);
    }

    // 4. BeanFactoryAware
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("4. 【Aware接口】BeanFactoryAware.setBeanFactory");
    }

    // 5. ApplicationContextAware
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("5. 【Aware接口】ApplicationContextAware.setApplicationContext");
    }

    // 6. @PostConstruct
    @PostConstruct
    public void postConstruct() {
        System.out.println("6. 【注解回调】@PostConstruct方法执行");
    }

    // 7. InitializingBean
    @Override
    public void afterPropertiesSet() {
        System.out.println("7. 【接口回调】InitializingBean.afterPropertiesSet");
    }

    // 8. 自定义init-method
    public void customInit() {
        System.out.println("8. 【自定义初始化】XML配置的init-method执行");
    }

    // 9. 业务方法
    public void doSomething() {
        System.out.println("9. 【业务方法】Bean使用中: " + this.name);
    }

    // 10. @PreDestroy
    @PreDestroy
    public void preDestroy() {
        System.out.println("10. 【注解回调】@PreDestroy方法执行");
    }

    // 11. DisposableBean
    @Override
    public void destroy() {
        System.out.println("11. 【接口回调】DisposableBean.destroy");
    }

    // 12. 自定义destroy-method
    public void customDestroy() {
        System.out.println("12. 【自定义销毁】XML配置的destroy-method执行");
    }
}
